package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.TypeAdapter;
import flc.ast.adapter.TypeMovieAdapter;
import flc.ast.bean.TypeBean;
import flc.ast.databinding.FragmentTypeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TypeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TypeFragment extends BaseNoModelFragment<FragmentTypeBinding> {

    private TypeAdapter typeAdapter, yearAdapter;
    private TypeMovieAdapter movieAdapter;
    private int typePos, yearPos;

    @Override
    protected int onCreate() {
        return R.layout.fragment_type;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSearch.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        typeAdapter = new TypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        mDataBinding.rvYearList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        yearAdapter = new TypeAdapter();
        mDataBinding.rvYearList.setAdapter(yearAdapter);
        yearAdapter.setOnItemClickListener(this);

        mDataBinding.rvTypeMovieList.setLayoutManager(new LinearLayoutManager(mContext));
        movieAdapter = new TypeMovieAdapter();
        mDataBinding.rvTypeMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivSearch://搜索
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_text));
                    return;
                }
                SearchActivity.key = key;
                startActivity(SearchActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == typeAdapter) {
            typeAdapter.getItem(typePos).setSelected(false);
            typePos = position;
            typeAdapter.getItem(position).setSelected(true);
            yearAdapter.getItem(yearPos).setSelected(false);
            getMovieHotData(typeAdapter.getItem(position).getHashid());
            typeAdapter.notifyDataSetChanged();
            yearAdapter.notifyDataSetChanged();
        } else if (adapter == yearAdapter) {
            yearAdapter.getItem(yearPos).setSelected(false);
            yearPos = position;
            yearAdapter.getItem(position).setSelected(true);
            typeAdapter.getItem(typePos).setSelected(false);
            getMovieHotData(yearAdapter.getItem(position).getHashid());
            typeAdapter.notifyDataSetChanged();
            yearAdapter.notifyDataSetChanged();
        } else if (adapter == movieAdapter) {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getTypeData();
        getYearData();
    }

    private void getTypeData() {
        StkResApi.getChildTagList(this, AppConfig.TYPE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<TypeBean> list = new ArrayList<>();
                        for (StkTagResBean stkTagResBean : result) {
                            list.add(new TypeBean(stkTagResBean.getName(), stkTagResBean.getHashid()));
                        }
                        list.get(0).setSelected(true);
                        typeAdapter.setList(list);
                        getMovieHotData(typeAdapter.getItem(0).getHashid());
                    }
                }
            }
        });
    }

    private void getYearData() {
        StkResApi.getChildTagList(this, AppConfig.YEAR_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<TypeBean> list = new ArrayList<>();
                        for (StkTagResBean stkTagResBean : result) {
                            list.add(new TypeBean(stkTagResBean.getName(), stkTagResBean.getHashid()));
                        }
                        yearAdapter.setList(list);
                    }
                }
            }
        });
    }

    private void getMovieHotData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieAdapter.setList(result);
                    }
                }
            }
        });
    }

}